package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Slf4j
@Component
public class DownOrUpListener {

    @Autowired
    private ApArticleConfigMapper ApArticleConfigMapper;

    @KafkaListener(topics = "news.up.or.down")
    public void getDownOrUpMessage(String message) {
        if (StringUtils.isBlank(message)) {
            log.warn("上下架接收到的信息为空");
            return;
        }
        //反序列化,获取参数
        Map map = JSON.parseObject(message, Map.class);
        if (CollectionUtils.isEmpty(map)) {
            log.warn("上下架接收到的信息为空");
            return;
        }
        Long articleId = (Long) map.get("articleId");
        Boolean enable = (Boolean) map.get("enable");

        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setIsDown(enable);

        LambdaQueryWrapper<ApArticleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleConfig::getArticleId, articleId);

        ApArticleConfigMapper.update(apArticleConfig, queryWrapper);
    }
}
